declare namespace r='http://rules.independentsi.org';
declare namespace sx="http://saxon.sf.net/";
declare namespace xsl="http://www.w3.org/1999/XSL/Transform";

declare variable $xslt_file external;
declare variable $xsltrules_home external;

let $xslt := doc(replace($xslt_file,'\\','/'))

let $nsnodes:=for $x in in-scope-prefixes($xslt/*)
	return sx:namespace-node($x, namespace-uri-for-prefix($x,$xslt/node()))
return <r:rules>
{
	attribute {'for'} {replace($xslt_file, '\\', '/')},
	attribute {'inputpath'} {"./data"},
	attribute {'inputfilepattern'} {"*.xml"},
	attribute {'outpath'} {"./output"},
	attribute {'xsi:schemaLocation'} {concat('http://rules.independentsi.org file:///', $xsltrules_home, '/xsds/xsltRules.xsd')},
	$nsnodes,
	comment {'
	define a preprocess element if the input document requires preprocessing before the tests can be executed.
	In the example below, we define two steps, an xslt step to add a namespace, and an xquery step to construct the document based on some static input..
	<r:preprocess>
			<r:step type="xslt">
				<r:xslt path="c:/AppWork/integration-trunk/NetbeansProjects/ReceiveB2BReturnedCalls/ctrlReceiveB2BReturnedCalls/src/xslB2BAddNameSpacesToReturned.xsl"/>
			</r:step>
			<r:step type="xquery">
				<r:xquery><![CDATA[
					declare namespace nm="http://xxxlater.co.uk/namemapping";
					declare namespace env="http://xml.netbeans.org/schema/B2BCallTranslationRequest";
					declare namespace nd="http://xxxlater.co.uk/job/namedattributedefinitions";
					declare namespace rc="x-schema:ReturnedCalls.xsd";
					let $nmd := doc("c:/AppWork/integration-trunk/NetbeansProjects/CommonServicesB2B/CommonServicesB2B_CA/src/jbiasa/config-deploy/B2BMapping.xxx.xml")
					let $jnad := doc("c:/AppWork/integration-trunk/Schemas/Common/xxxJobNamedAttributes.xml")
					return 
					<env:B2BCallTranslationRequest>
						{ (: This returns a sequence of the required elements to make up the request document :)
							(/rc:Calls,
							<nm:NameMapping>{$nmd/nm:NameMapping/nm:configuration[@name="incoming"]}</nm:NameMapping>,
							$jnad)
						}
					</env:B2BCallTranslationRequest>
					]]>
					</r:xquery>
			</r:step>
		</r:preprocess>
	'},
	comment {'
	define a r:include to import a series of pre-defined rules
	<r:include path="C:/Projects/xsltrules/lib/b2bcall2job/jnaMapping.xml"/>
	'},
	comment {"
	define a macro to use as a shorthand for repetitive xpaths
	@name is the name of the macro, @definition is an xpath where $_1_ is the first argument passed to the macro when called in the form:
	macro:jna-get-attribute(SomeAttributeName)
	<r:macro name='jna-get-attribute' definition='/job:xxxJob/job:jobDetails/job:additionalData/lib:attribute[@name='$_1_']/lib:value/lib:typedValue/node()/text()'/>
	"},
	comment {'
		r:rules to apply to the input and output documents to determine success or failure
	'}
}
</r:rules>
